package com.evon.yardmanagement.ui.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.evon.yardmanagement.dao.HibernateHelper;
import com.evon.yardmanagement.dao.HibernateUtil;

public class OpenSessionInViewFilter implements Filter {
	 /**
     * {@inheritDoc}
     */
   public void destroy() {
       // no-op
   }
 
   /**
     * {@inheritDoc}
     */
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
           ServletException {
       // set up a new session at beginning of request and close it afterwards
       try {
           getHibernateHelper().openAndBindSession();
           chain.doFilter(request, response);
       } finally {
           getHibernateHelper().unbindAndCleanupSession();
       }
   }
 
   /**
     * Applications using this filter need to implement this method to help get the instance to your hibernate helper.
     * @return the hibernate helper.
     */
   public HibernateHelper getHibernateHelper() {
       return HibernateUtil.getHibernateHelper();
   }
 
   /**
     * {@inheritDoc}
     */
   public void init(FilterConfig arg0) throws ServletException {
       // no-op
   }
}